#include "headers/Executor.h"

#include "headers/Tracker.h"
#include "headers/Disco.h"
#include "headers/Buffer.h"
#include "headers/Operacoes.h"

#include <time.h>
#include <stdlib.h>


//int ordem[TAMANHO_EXECUTOR]; //Atualmente 10. Setado no Config.h

void Executar() {

	CriarTracker();
	//Vari�veis para pegar posi��o no tracker
	char* local = malloc(sizeof(char));
	int* offset = malloc(sizeof(int));

	char* randOP = malloc(sizeof(char));
	int* randGID = malloc(sizeof(int));

	srand(time(NULL));
	int count_miss = 0;
	int count_hits = 0;
	int count_alter = 0;
	Pagina* pagTemp; //= (Pagina*) malloc(sizeof(Pagina));

	initRemainingOperations();

	int i;
	for (i = 0; i < TOTAL_OPERATIONS; i++) {
		pagTemp = (Pagina*) malloc(sizeof(Pagina));
		//printf("Id desejado: \n");
		//scanf("%d", &randGID);
		//randGID = rand() % (NUMERO_DE_PAGINAS - 1); //int aleatório entre 0 e NUMERO_DE_PAGINAS-1.

		//printf("Id Operacao: %i \n",i);


		generateOperations(randGID,randOP);

		trac_LocalizarPagina(*randGID, local, offset); //Procura no tracker onde est� a p�gina.

		/*printf("Operacao desejada: \n");
		scanf("%d", &randOP);*/

		/*int num = rand() % 2;

		if(num == 1)
			randOP = 65;
		else
			randOP = 76;*/

		switch(*randOP){//W = 87, R = 82 (alterar, ler)
			case 87: //Alterar (wright)

					if(*local != NULL){//Ou seja, pagina esta no buffer
						count_hits++;
						buff_AlterarPagina(*offset);

						/*printf("\n----Alteracao: \n");
						printf("GID:      %d\n", pagTemp->id);
						printf("Conteudo: %s\n", pagTemp->conteudo);*/
					}else{
						count_miss++;
						CriarPagina(disc_LerPagina(*randGID, "paginas.txt"), pagTemp); //Le do disco a pagina
						buff_EscreverPagina(pagTemp); //Grava no buffer a pagina
						buff_AlterarPagina(*offset); //Altera a pagina

						/*printf("\n----Alteracao: \n");
						printf("GID:      %d\n", pagTemp->id);
						printf("Conteudo: %s\n", pagTemp->conteudo);*/
						free(pagTemp);
					}

			    *offset = -1;
				*local = NULL;
				count_alter++;
				break;
			case 82: //Ler (read)

					if(*local != NULL){//Ou seja, pagina esta no buffer.
						count_hits++;
						pagTemp = buff_LerPagina(*offset); //HitDate em LerPagina

						/*printf("\n----Leitura: \n");
						printf("GID:      %d\n", pagTemp->id);
						printf("Conteudo: %s\n", pagTemp->conteudo);*/

					}else{
						count_miss++;
						CriarPagina(disc_LerPagina(*randGID, "paginas.txt"), pagTemp); //Le do disco a pagina
						buff_EscreverPagina(pagTemp); //Grava no buffer a pagina | HitDate em EscreverPagina.

						/*printf("\n----Leitura: \n");
						printf("GID:      %d\n", pagTemp->id);
					    printf("Conteudo: %s\n", pagTemp->conteudo);*/
						free(pagTemp);
					}
			    *offset = -1;
				*local = NULL;
				break;
		}

       /* printf("--------------------------------------\n");
        printf("count_hits: %i\n",count_hits);
        printf("count_miss: %i\n",count_miss);
        printf("==========================================================\n\n\n\n\n\n");*/

	}


	printf("Total de Operacoes: %d", TOTAL_OPERATIONS);
	printf("\n% Operacoes Escrita: %i", TOTAL_WRITE_OPERATIONS_PERCENT);
	printf("\n-------------------------------------------------------");
	printf("\n% Total de Páginas (# paginas BD): %i", NUMERO_DE_PAGINAS);
	printf("\n% Tamanho do Buffer (# páginas): %i", CAPACIDADE_DO_BUFFER);
	printf("\n-------------------------------------------------------");
	printf("\nChamadas LRU: %d", count_LRU);
	printf("\nChamadas LRU - Dirty Pages: %d", count_LRU_dirty);
	printf("\nChamadas LRU - Non-Dirty Pages: %d", count_LRU - count_LRU_dirty);
	printf("\n-------------------------------------------------------");
	printf("\nMiss: %d",count_miss);
	printf("\nHits: %d",count_hits);
	printf("\n-------------------------------------------------------");


}

